﻿/**
* @authors <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.ng.ResHelp {
	
	import flash.utils.Dictionary;

	public class c_LoadingChannel {

		private var __ID:String;
		private var __Loaders:Dictionary;
		private var __ProgressListener:Function;
		private var __CompleteListener:Function;
		private var __StartedListener:Function;

		public function c_LoadingChannel(p_ID:String) {
			__ID = p_ID;
			__Loaders = new Dictionary();
		}
		
		public function get Loaders():Dictionary {
			return __Loaders;
		}
		public function getLoaderById(id:String):c_Loader {
			return __Loaders[id];
		}
		
		public function setProgressListener(p_ProgressListener:Function):void {
			__ProgressListener = p_ProgressListener;
		}
		public function setStartedListener(p_StartedListener:Function):void {
			__StartedListener = p_StartedListener;
		}
		public function setCompleteListener(p_CompleteListener:Function):void {
			__CompleteListener = p_CompleteListener;
		}
		
		public function register(p_Element:c_Loader):void {
			//id of c_Loader objects is unique
			if (__Loaders[p_Element.id] == null) {
				__Loaders[p_Element.id] = p_Element;
			}
		}		
		
		public function OnElementProgress(p_Element:c_Loader):void {
			if (__ProgressListener) {				
				__ProgressListener.call(this, __ID, p_Element);
			}
			if (__StartedListener) {
				var l_IsStarted:Boolean = true;				
				for each(var e:c_Loader in __Loaders) {
					l_IsStarted = l_IsStarted && e.isStarted;
				}
				if(l_IsStarted) {
					__StartedListener.call(this, __ID);					
					__StartedListener = null;
				}
			}
		}		
		public function OnElementLoaded(p_Element:c_Loader):void {			
			if (__CompleteListener) {
				var l_IsLoaded:Boolean = true;
				
				for each(var e:c_Loader in __Loaders) {
					l_IsLoaded = l_IsLoaded && e.isLoaded;
				}
				if(l_IsLoaded) {
					__CompleteListener.call(this, __ID);
					__ProgressListener = null;
					__CompleteListener = null;
				}
			}
		}		
	}
}